﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AssetAllocationSystem.Models;
using NHibernate;
using NHibernate.Criterion;

namespace AssetAllocationSystem.Repositories
{
    public class CompanyRepository : RepositoryBase<Company>
    {
        private ISession session;
        public CompanyRepository(ISession session) : base(session)
        {
            this.session = session;
        }

        //public IList<Company> FindById(Guid companyId)
        //{
        //    ISession session = base.CreateSession();

        //    var company = DetachedCriteria
        //        .For<Company>()
        //        .Add(Restrictions.Eq("Id", companyId))
        //        .GetExecutableCriteria(session)
        //        .List<Company>();

        //    return company;
        //}

        public IList<Company> FindByName(string name, bool isExactMatch)
        {
            if (!isExactMatch)
            {
                var companies =
                    DetachedCriteria
                        .For<Company>()
                        .Add(Restrictions.Like("Name", name))
                        .GetExecutableCriteria(session)
                        .List<Company>();

                return companies;
            }
            else
            {
                var companies =
                    DetachedCriteria
                        .For<Company>()
                        .Add(Restrictions.Eq("Name", name))
                        .GetExecutableCriteria(session)
                        .List<Company>();

                return companies;
            }
            
        }

        public IList<Company> FindAll()
        {
            var companies =
                DetachedCriteria
                    .For<Company>()
                    .GetExecutableCriteria(session)
                    .List<Company>();

            return companies;
        }
    }
}